﻿using MES_SystemUI.Context;
using MES_SystemUI.Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace MES_SystemUI.UI.DataDictUI.ProductsDataDic
{
    // 产品表
    public partial class ProductsListForm : MES_SystemUI.UI.DataDictUI.DataBaseForm
    {
        public ProductsListForm()
        {
            InitializeComponent();
            this.Load += ProductsListForm_Load;
        }

        private void ProductsListForm_Load(object sender, EventArgs e)
        {
            BindDataGridView();
        }
        public override void GetDataBind()
        {
            using (var ct = new Context.MES_SystemContext())
            {
                var que = from P in ct.products.ToList()
                          join Ps in ct.productSpecs on P.ProductSpecId equals Ps.ProductSpecId
                          join Pu in ct.units on P.ProductUnitId equals Pu.UnitId
                          join U0 in ct.users on P.MaintainerId equals U0.UserId
                          join Pc in ct.productCategories on P.ProductCategotyId equals Pc.ProductCategoryId
                          join U1 in ct.users on P.CreateUserId equals U1.UserId
                          join U2 in ct.users on P.LastUpdateUserId equals U2.UserId into AllPc
                          from pc in AllPc.DefaultIfEmpty()
                          select new
                          {
                              序号 = P.ProductId,
                              产品编码 = P.ProductCode,
                              产品名称 = P.ProductName,
                              产品类型 = Pc.ProductCategoryName,
                              维护人 = U0.UserName,
                              维护时间 = P.MaintenanceTime,
                              规格 = Ps.ProductSpecName,
                              单位 = Pu.UnitName,
                              图片 = MES_SystemUI.Helpers.ImageHelper.ByteArrayToImage(P.ProductImage),
                              状态 = P.Status == 0 ? "启用" : "禁用",
                              创建人 = U1.UserName,
                              创建时间 = P.CreateTime,
                              最后修改人 = pc != null ? pc.UserName : null,
                              最后修改时间 = P.LastUpdateTime,
                          };
                if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
                {
                    que = que.Where(x => Regex.IsMatch(x.产品名称, textBox1.Text));
                }
                if (!string.IsNullOrEmpty(dateTimePicker1.Text.Trim()))
                {
                    //DateTime dateTime1 = DateTime.Parse(dateTimePicker1.Value.ToString("yyyy-MM-dd") + " 00:00:00");
                    DateTime dateTime1 = DateTime.Parse(dateTimePicker1.Text + " 00:00:00");
                    que = que.Where(x => x.创建时间 >= dateTime1);
                }
                if (!string.IsNullOrEmpty(dateTimePicker2.Text.Trim()))
                {
                    //DateTime dateTime2 = DateTime.Parse(dateTimePicker2.Value.ToString("yyyy-MM-dd") + " 23:59:59");
                    DateTime dateTime2 = DateTime.Parse(dateTimePicker2.Text + " 23:59:59");
                    que = que.Where(x => x.创建时间 <= dateTime2);
                }
                if (comboBox1.Text != "全部")
                {
                    que = que.Where(x => x.状态 == comboBox1.Text);
                }
                Page = que.Count() % MainForm.SystemDataConfig.pageNumber == 0 ? que.Count() / MainForm.SystemDataConfig.pageNumber : que.Count() / MainForm.SystemDataConfig.pageNumber + 1;
                que = que.OrderByDescending(k => k.序号);
                que = que.Skip((NumberPage - 1) * MainForm.SystemDataConfig.pageNumber).Take(MainForm.SystemDataConfig.pageNumber);
                // 绑定数据
                dataGridView1.Columns.Clear();
                dataGridView1.DataSource = que.ToList();


            }
        }
        public override void OpenExcelCore(DataTable dt, MES_SystemContext c, int i)
        {
            Domain.Product mode = new Domain.Product()
            {
                ProductCode = dt.Rows[i][0].ToString(),
                ProductName = dt.Rows[i][1].ToString(),
                MaintainerId = int.Parse(dt.Rows[i][2].ToString()),
                MaintenanceTime = DateTime.Parse(dt.Rows[i][3].ToString()),
                ProductCategotyId = int.Parse(dt.Rows[i][4].ToString()),
                ProductSpecId = int.Parse(dt.Rows[i][5].ToString()),
                ProductUnitId = int.Parse(dt.Rows[i][6].ToString()),
                Status = int.Parse(dt.Rows[i][7].ToString()),
                CreateUserId = int.Parse(dt.Rows[i][8].ToString()),
            };
            c.products.Add(mode);
        }
        public override void StateCore(DataGridViewCellMouseEventArgs e, MES_SystemContext c, int state)
        {
            var Pc = c.products.Find(int.Parse(dataGridView1.Rows[e.RowIndex].Cells["序号"].Value.ToString()));
            Pc.Status = state;
        }
    }
}
